package com.ozveri.menu;


import java.util.ArrayList;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.util.Log;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;

public class AnimatedMainScreenFactory {

	private int numberOfItems;
	private Context ctx;
	private WindowManager WndwMng;
	private int screenWidth;
	private int screenHeight;
	public Point defaultSize;
	private final double ROW_CONSTANT = 3.0;
	public ArrayList<View> mainButtons;
	public ArrayList<Point> mainButtonPoints;
	public ArrayList<Point> mainButtonUsable;


	public AnimatedMainScreenFactory( int mainScreenItemNumber, Context context, WindowManager manager ) {
		numberOfItems = mainScreenItemNumber;
		ctx = context;
		WndwMng = manager;
		mainButtons = new ArrayList<View>(mainScreenItemNumber);
		mainButtonPoints = new ArrayList<Point>(mainScreenItemNumber);
		mainButtonUsable = new ArrayList<Point>(mainScreenItemNumber);
	}

	public View createMainScreen()
	{		
		RelativeLayout mainLayout = new RelativeLayout(ctx);

		mainLayout.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));

		//using R.color.xxx was a disaster...
		//when reaching R other than a activity class pass context and use .get resources
		mainLayout.setBackgroundColor(ctx.getResources().getColor(R.color.blue));

		//ok we create a stupid blue background lets add some layouts using number of items
		int rowNumber = (int)Math.ceil(numberOfItems/ROW_CONSTANT);


		//TODO we must take some notificationbarssize too but later

		Log.w("Width",""+WndwMng.getDefaultDisplay().getWidth());  // Display width
		Log.w("Height",""+WndwMng.getDefaultDisplay().getHeight()); // Display height

		if(WndwMng.getDefaultDisplay().getWidth()>WndwMng.getDefaultDisplay().getHeight()){
			screenWidth = WndwMng.getDefaultDisplay().getWidth();
			screenHeight = WndwMng.getDefaultDisplay().getHeight();			
		}
		else
		{
			screenWidth =WndwMng.getDefaultDisplay().getHeight();
			screenHeight =  WndwMng.getDefaultDisplay().getWidth();	
		}

		int usableHeight = (screenHeight-150)/rowNumber;


		//		LayoutInflater inflater = (LayoutInflater)ctx.getSystemService
		//			      (Context.LAYOUT_INFLATER_SERVICE);
		//		
		//		View view = inflater.inflate(ctx.getResources().getLayout(R.layout.mainbutton), null);
		//		mainLayout.addView(view,450,400);

		int pictureHeight =  ((usableHeight)/4*3)-20;
		int TextHeight =  (usableHeight)/4;


		//TODO 4 must be a special case
		for (int i = 0; i < rowNumber; i++) {
			int rowItemNumber = (numberOfItems-(i*3)>2?3:(numberOfItems-(i*3)));
			for (int j = 0; j <rowItemNumber ; j++) { //TODO need to modify this to show hebele
				int usableWidth = (int)((double)screenWidth/rowItemNumber)-20;
				
				if(i==0&&j==0)
				{
					defaultSize = new Point();
					defaultSize.x = usableWidth;
					defaultSize.y = usableHeight;
				}
				
				ImageView img = new ImageView(ctx);
				//LinearLayout.LayoutParams imgParams = new LinearLayout.LayoutParams(usableWidth, pictureHeight);
				//img.setLayoutParams(imgParams);
				img.setImageResource(R.drawable.ic_launcher);
				img.setScaleType(ScaleType.CENTER_INSIDE);
				img.setAdjustViewBounds(true);
				img.setPadding(0, 10, 0, 10);		


				AutoResizeTextView text = new AutoResizeTextView(ctx);

				text.resizeText(usableWidth, TextHeight);
				text.setGravity(Gravity.CENTER_HORIZONTAL);


				LinearLayout midLayout = new LinearLayout(ctx);
				midLayout.setOrientation(android.widget.LinearLayout.VERTICAL);
				LayoutParams layoutParams = new LayoutParams(usableWidth, LayoutParams.WRAP_CONTENT);
//				layoutParams.height =  
//				usableWidth,usableHeight);
//				
				layoutParams.setMargins((int)(usableWidth*j+(rowItemNumber/2.0*20)), 50+(i*usableHeight), 0, 0);
				
				text.setText("Deneme Falan "+(50+(i*usableHeight))+" ");
				midLayout.setLayoutParams(layoutParams);
				midLayout.setBackgroundColor(Color.BLACK);


				midLayout.addView(img);
				midLayout.addView(text);
				mainButtons.add(midLayout);
				Point midButtonPoint = new Point((int)(usableWidth*j+(rowItemNumber/2.0*20)), 50+(i*usableHeight));
				mainButtonPoints.add(midButtonPoint);
				mainButtonUsable.add(new Point(usableWidth,usableHeight));
				mainLayout.addView(midLayout);

			}

		}
		return mainLayout;
	}

}
